﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using SMALLERP.PO;
using ThoughtWorks.QRCode.Codec; 

namespace SMALLERP.PO
{
    public partial class Print : Form

    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        //AppPO POForm;
        public delegate void ChangeZXS(object sender, EventArgs e);

        public Print()
        {
            InitializeComponent();
        }

        public event SMALLERP.PO.Print.ChangeZXS ZXS;

        private void Print_Load(object sender, EventArgs e)
        {

            this.textBox1.Text = "0";
            this.textBox2.Text = "0";
            this.textBox3.Text = "0";
            this.textBox4.Text = QJBL.invname.ToString();
            this.textBox5.Text = QJBL.invcode.ToString();
            this.textBox6.Text = QJBL.invspec.ToString();
            //this.textBox7.Text = QJBL.invfig.ToString();
            this.textBox8.Text = DateTime.Now.ToString();
            this.textBox13.Text = QJBL.poDDH.ToString();
            //this.textBox9.Text ="A0706001";
            //this.textBox10.Text = PropertyClass.OperatorCode;
            //this.textBox11.Text = PropertyClass.OperatorName;
            //this.textBox11.Text = "";
            
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox10.Text = textBox2.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            long xs = long.Parse(textBox1.Text);
            double dx = double.Parse(textBox2.Text);
            string xds = QJBL.poXDS.ToString();
            double xd = double.Parse(xds);
            string zxs = QJBL.poYZX.ToString();
            double zx = double.Parse(zxs);
            string yjs = QJBL.poYJS.ToString();
            double yj = double.Parse(yjs);
            string dys = QJBL.poDYS.ToString();
            double dy = double.Parse(dys);
            string yts = QJBL.poYTS.ToString();
            double yt = double.Parse(yts);
            if (textBox1.Text == "0")
            {
                try
                {
                    MessageBox.Show("箱数不能为0！","软件提示");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }

            if (textBox2.Text == "0")
            {
                try
                {
                    MessageBox.Show("每箱数量不能为0！", "软件提示");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            if (textBox19.Text == "")
            {
                try
                {
                    MessageBox.Show("请输入库位！", "软件提示");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
                if ((xs * dx) > (xd + yt - yj - dy))
                {
                    try
                    {
                        MessageBox.Show("装箱总数不能超过可交数量！,现在还能交" + (xd + yt - yj - dy) + "", "软件提示");
                        return;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                    finally
                    {
                    }
                }
                if ((xs * dx) > (xd + yt - zx))
                {
                    try
                    {
                        MessageBox.Show("装箱总数不能超过可装箱数！,现在还能装" + (xd + yt - zx) + "", "软件提示");
                        return;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                    finally
                    {
                    }
                }

                //用默认的所有者使用通用的对话框、
                //printPreviewDialog1.Document = this.printDocument1;//设置打印的文档
                //printPreviewDialog1.ShowDialog();//将窗体显示为模式对话框，并将当前活动窗口设置为所有者
                string nf ="D";
                string strSql = null;

                strSql = "SELECT RTRIM(XC002) XC002 FROM COPXC WHERE XC001 = SUBSTRING(CONVERT(CHAR(8),GETDATE(),112),1,4)";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);
                    sdr1.Read();
                   nf = sdr1["XC002"].ToString();
                 
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                    sdr1.Close();

                }

                for (int i = 0; i < xs; i++)
                {
                    
                    strSql = "SELECT ISNULL('" + nf + "'+RIGHT(CONVERT(CHAR(8),GETDATE(),112),4)+RIGHT('000'+RTRIM(cast(MAX(CAST(SUBSTRING(XA002,6,4) as bigint))+1 AS CHAR(4))),4) ,";
                    strSql += "'" + nf + "'+RIGHT(CONVERT(CHAR(8),GETDATE(),112),4)+'0001') AS XH FROM COPXA ";
                    strSql += "WHERE XA002 LIKE '" + nf + "'+RIGHT(CONVERT(CHAR(8),GETDATE(),112),4)+'%'";
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);
                        sdr1.Read();
                        textBox9.Text = sdr1["XH"].ToString();
                        barcodeControl1.Value = textBox9.Text;
                          

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                    finally
                    {
                        sdr1.Close();

                    }



                    //获取帐套数据库
                    string DataBase2 = OperatorFile.GetIniFileString("DataBase", "DataBase2", "",
                                                                  Application.StartupPath + "\\SCM.ini");

                    strSql = "INSERT INTO COPXA SELECT  '" + DataBase2 + "','" + PropertyClass.OperatorCode + "','',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'','',1,RIGHT(CONVERT(CHAR(8),GETDATE(),112)+'0000000000000000000000000000000000000000000000000'+RTRIM(ISNULL(CAST(CAST(MAX(RIGHT(RTRIM(XA001),10)) AS NUMERIC(10))+1 AS VARCHAR(10)),'1')),60),";
                    strSql += "'" + textBox9.Text + "','" + QJBL.poDB.ToString() + "','" + QJBL.poNO.ToString() + "','" + QJBL.invcode.ToString() + "','" + textBox5.Text + "','" + textBox6.Text + "','','" + textBox13.Text + "',''," + textBox12.Text + ",0," + textBox3.Text + ",'','','','','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox8.Text + "'),112),'','','','N','N','N','','','','" + textBox19.Text + "','" + textBox10.Text + "','',0,0,0,'" + QJBL.poXH.ToString() + "','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0,'','','','','',0,'','','','' FROM COPXA WHERE XA001 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                            //this.printDocument1.Print();
                            //MessageBox.Show("打印成功！", "软体提示");
                            //foreach (PaperSize paper in printDoc.PrinterSettings.PaperSizes)
                            //{
                            //    if (paper.PaperName == "自定义纸张名称")
                            //    {
                            //        printDoc.DefaultPageSettings.PaperSize = paper;
                            //        break;
                            //    }
                            //}
                            printPreviewDialog1.Document = this.printDocument1;//设置打印的文档
                            printPreviewDialog1.ShowDialog();//将窗体显示为模式对话框，并将当前活动窗口设置为所有者
                        }
                        else
                        {
                            MessageBox.Show("打印失败！", "软体提示");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软体提示");
                        throw ex;
                    }

                }

               
                MessageBox.Show("打印成功！", "软体提示");
                //this.FormClosed += new EventHandler(AppPO_Activated);
                ZXS(sender, e);
                Close();
            }
        

       

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {      
            e.HasMorePages=false;
            Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
            panel1.DrawToBitmap(_NewBitmap, new Rectangle(2, 2, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 2, 2, _NewBitmap.Width, _NewBitmap.Height);

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

       

       

        private void textBox8_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox19_TextChanged(object sender, EventArgs e)
        {
            textBox13.Text = textBox19.Text;

        }

        private void textBox19_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                textBox13.Text = textBox19.Text;
                }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            finally
            {
               
            }
            }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            textBox12.Text = textBox3.Text;
        }

        private void printPreviewDialog1_Load(object sender, EventArgs e)
        {

        }

        }  
           
    }

